package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.*;
import com.design.softwaredesign.front.GoodInfo;
import com.design.softwaredesign.mapper.GoodMapper;
import com.design.softwaredesign.mapper.IwantMapper;
import com.design.softwaredesign.service.GoodService;
import com.design.softwaredesign.vo.DataVo;
import com.design.softwaredesign.vo.GoodVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/10/27 16:20
 */
@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private IwantMapper iwantMapper;

    public DataVo getGoodsCategory() {
        DataVo dataVo = new DataVo();
        try{
            List<GoodCategory> goodCategoryList = new ArrayList<>();
            goodCategoryList = goodMapper.getGoodsCategory();
            dataVo.setCode(200);
            dataVo.setCount((long) goodCategoryList.size());
            dataVo.setMsg("商品类型获取成功");
            dataVo.setData(goodCategoryList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("商品类型获取失败");
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getGoodsCategoryListData(Integer pageNum, Integer pageSize, String query, String userId) {
        DataVo dataVo = new DataVo();
        List<GoodCategory> goodCategoryList = new ArrayList<>();
        try {
            goodCategoryList = goodMapper.getGoodsCategory();
            List<Good> goodslist = null;
            for(int k = 0; k < goodCategoryList.size(); k++) {
                PageHelper.startPage(pageNum, pageSize);
                goodslist = goodMapper.getGoodList(goodCategoryList.get(k).getGoodCategoryId(), query);
                PageInfo<Good> goodPageInfo = new PageInfo<>(goodslist);
                List<GoodVo> goodVoList = new ArrayList<>();
                Good good = null;
                for(int i = 0; i < goodslist.size(); i++) {
                    GoodVo goodVo = new GoodVo();
                    good = goodslist.get(i);
                    BeanUtils.copyProperties(good, goodVo);
                    if(!good.getImageUrl().equals("")) {
                        String []imageUrls = good.getImageUrl().split(";");
                        List<String> imageUrlList = new ArrayList<>();
                        for(String imageUrl : imageUrls) {
                            imageUrlList.add(imageUrl);
                        }
                        goodVo.setImageUrlList(imageUrlList);
                    }
                    if(userId != null && !userId.equals("")) {
                        if(iwantMapper.existIwant(goodVo.getGoodId(), userId) == 1) {
                            goodVo.setInMyWant(true);
                        }else {
                            goodVo.setInMyWant(false);
                        }
                    }else {
                        goodVo.setInMyWant(false);
                    }
                    goodVoList.add(goodVo);
                }
                goodCategoryList.get(k).setListdata(goodVoList);
            }
            dataVo.setCode(200);
            dataVo.setCount((long) goodCategoryList.size());
            dataVo.setMsg("商品类型获取成功");
            dataVo.setData(goodCategoryList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("商品类型获取失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }

    @Override
    public DataVo addGood(GoodInfo goodInfo) {
        DataVo dataVo = new DataVo();
        Good good = new Good();
        try {
            BeanUtils.copyProperties(goodInfo, good);
            good.setGoodState("待出售");
            SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化当前系统日期
            String postTime = dateFm.format(new java.util.Date());
            good.setPostTime(postTime);
            goodMapper.addGood(good);
            dataVo.setCode(200);
            dataVo.setMsg("商品上传成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("商品上传失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }

    @Override
    public void addImage(String goodId, String url) {
        GoodImages goodImages = goodMapper.selectGoodImages(goodId);
        url += ";";
        if(goodImages == null) {
            goodImages = new GoodImages();
            goodImages.setGoodId(goodId);
            goodImages.setImageUrl(url);
            goodMapper.addGoodImages(goodImages);
        } else {
            String images = goodImages.getImageUrl();
            images += url;
            goodImages.setImageUrl(images);
            goodMapper.updateImages(goodImages);
        }
    }

    @Override
    public DataVo getGoodsList(Integer goodCategoryId, Integer pageNum, Integer pageSize, String query, String userId) {
        DataVo dataVo = new DataVo();
        List<Good> goodslist = null;
        try {
            PageHelper.startPage(pageNum, pageSize);
            goodslist = goodMapper.getGoodList(goodCategoryId, query);
            PageInfo<Good> goodPageInfo = new PageInfo<>(goodslist);
            List<GoodVo> goodVoList = new ArrayList<>();
            Good good = null;
            for(int i = 0; i < goodslist.size(); i++) {
                GoodVo goodVo = new GoodVo();
                good = goodslist.get(i);
                BeanUtils.copyProperties(good, goodVo);
                if(!good.getImageUrl().equals("")) {
                    String []imageUrls = good.getImageUrl().split(";");
                    List<String> imageUrlList = new ArrayList<>();
                    for(String imageUrl : imageUrls) {
                        imageUrlList.add(imageUrl);
                    }
                    goodVo.setImageUrlList(imageUrlList);
                }
                goodVoList.add(goodVo);
            }
            dataVo.setCode(200);
            dataVo.setCount(goodPageInfo.getTotal());
            dataVo.setMsg("获取商品列表成功");
            dataVo.setData(goodVoList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("获取商品列表失败");
            e.printStackTrace();
        } finally {
            return  dataVo;
        }
    }

    @Override
    public DataVo updateGoodInfo(GoodInfo goodInfo) {
        DataVo dataVo = new DataVo();
        try {
            goodMapper.updateGoodInfo(goodInfo);
            dataVo.setCode(200);
            dataVo.setMsg("商品修改成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("商品修改失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getGoodByGoodId(String goodId, String userId) {
        DataVo dataVo = new DataVo();
        try {
            Good good = goodMapper.selectGoodById(goodId);
            GoodVo goodVo = new GoodVo();
            List<GoodVo> goodVoList = new ArrayList<>();
            BeanUtils.copyProperties(good, goodVo);
            if(!good.getImageUrl().equals("")) {
                String []imageUrls = good.getImageUrl().split(";");
                List<String> imageUrlList = new ArrayList<>();
                for(String imageUrl : imageUrls) {
                    imageUrlList.add(imageUrl);
                }
                goodVo.setImageUrlList(imageUrlList);
            }
            if(userId != null && !userId.equals("")) {
                if(iwantMapper.existIwant(goodVo.getGoodId(), userId) == 1) {
                    goodVo.setInMyWant(true);
                }else {
                    goodVo.setInMyWant(false);
                }
            }else {
                goodVo.setInMyWant(false);
            }
            goodVoList.add(goodVo);
            dataVo.setCode(200);
            dataVo.setMsg("获取商品信息成功");
            dataVo.setData(goodVoList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("获取商品信息失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo deleteGoodInfoById(String goodId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("闲置删除失败");
        try {
            if(goodMapper.deleteGoodInfoById(goodId) == 1) {
                dataVo.setCode(200);
                dataVo.setMsg("闲置删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo changeGoodStateById(String goodId, String phone) {
        DataVo dataVo = new DataVo();
        Good good;
        dataVo.setCode(202);
        dataVo.setMsg("状态修改失败");
        String buyerId = "";
        try {
            List<User> buyerList = goodMapper.getGoodInIwantBuyer(goodId);
            if(buyerList.size() == 0) {
                dataVo.setMsg("此商品还未有买家");
                dataVo.setCode(202);
                throw new Exception();
            } else {
                int flag = 1;
                for(User user: buyerList) {
                    if(user.getPhone().equals(phone)) {
                        flag = 0;
                        buyerId = user.getUserId();
                        break;
                    }
                }
                if(flag == 1) {
                    dataVo.setMsg("此手机号购买方未将商品加入我想要中");
                    dataVo.setCode(202);
                    throw new Exception();
                }
            }
            good = goodMapper.selectGoodById(goodId);
            if(good.getGoodState().equals("待出售")){
                good.setGoodState("已出售");
                if(goodMapper.updateGoodState(good) == 1) {
                    IwantTrace iwantTrace = new IwantTrace();
                    iwantTrace.setUserId_buyer(buyerId);
                    iwantTrace.setGoodId(goodId);
                    iwantMapper.deleteIwantTrace(iwantTrace);
                    TransactionLog transactionLog = new TransactionLog();
                    transactionLog.setTransactionId(UUID.randomUUID().toString());
                    transactionLog.setGoodId(goodId);
                    transactionLog.setUserIdBuyer(buyerId);
                    transactionLog.setUserIdSeller(good.getUserIdSeller());
                    goodMapper.addTransationLog(transactionLog);
                    dataVo.setCode(200);
                    dataVo.setMsg("交易成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }
}
